package com.innoking.docsys.doc.controller;

import com.alibaba.fastjson.JSONObject;
import com.innoking.docsys.doc.service.GetOriginalService;
import com.innoking.docsys.utils.JsonToOtherUtil;
import com.innoking.docsys.utils.OtherPojo;
import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName GetOriginalController
 * @Description 档案浏览获取原文
 * @Author wr
 * @Date 2019/9/9 000909:46
 */
@RestController
@RequestMapping("/getOriginal")
public class GetOriginalController {

    Logger logger= LoggerFactory.getLogger(GetOriginalController.class);

    @Autowired
    private GetOriginalService getOriginalService;

    /**
     * 获取原文
     * id 数据表ID
     *type 所在数据表（volItem：卷内件，sinItem；单件）
     * @return
     */
    @PostMapping("/view")
    @ResponseBody
    public ResultObjVO view(@RequestBody String data) {
        logger.info("***获取原文进入");
        OtherPojo ob = JsonToOtherUtil.rtOtherString(data);
        String s = ob.getObj().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String id = jsonObject.get("id").toString();
        String type = jsonObject.get("type").toString();
        String url = getOriginalService.viewOriginal(id, type);
        logger.info("***获取原文结果："+url);
        if(StringUtils.isBlank(url)){
            return ResultUtil.error("入参为空或未获取到原文地址");
        }
        return ResultUtil.success( url );
    }

}
